package iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.dal;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabase;

import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.database.DBHelper;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.entity.Trace;

public class TraceInfoDAL {
    private final DBHelper dbHelper;
    private static final String tableName = "TraceInfo";

    public TraceInfoDAL(Context context) {
        dbHelper = new DBHelper(context, false);
    }

    public long addNewTrace(Trace newTrace) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("productId", newTrace.getProductId());
        values.put("producerId", newTrace.getProducerId());
        values.put("producerImg", newTrace.getProducerImg());

        try {
            return db.insertOrThrow(tableName, null, values);
        } catch (SQLiteConstraintException e) {
            return -1;
        } finally {
            db.close();
        }
    }

    public long updateTraceByDealer(Trace newTrace) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("productId", newTrace.getProductId());
        values.put("dealerId", newTrace.getDealerId());
        values.put("dealerImg", newTrace.getDealerImg());

        try {
            return db.update(tableName, values, "productId = ?", new String[]{newTrace.getProductId()});
        } catch (SQLiteConstraintException e) {
            return -1;
        } finally {
            db.close();
        }
    }

    public long updateTraceByConsumer(Trace newTrace) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("productId", newTrace.getProductId());
        values.put("consumerId", newTrace.getConsumerId());
        values.put("consumerImg", newTrace.getConsumerImg());

        try {
            return db.update(tableName, values, "productId = ?", new String[]{newTrace.getProductId()});
        } catch (SQLiteConstraintException e) {
            return -1;
        } finally {
            db.close();
        }
    }

    public Trace getTraceability(String productId) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        String[] columns = {"traceId", "productId", "producerId", "producerImg", "dealerId", "dealerImg", "consumerId", "consumerImg"};
        String selection = "productId = ?";
        String[] selectionArgs = {productId};

        Cursor cursor;
        try {
            cursor = db.query(
                    tableName,    // 表名
                    columns,       // 要查询的列
                    selection,     // WHERE 条件
                    selectionArgs, // 参数绑定
                    null,          // groupBy
                    null,          // having
                    null           // orderBy
            );

            if (cursor != null && cursor.moveToFirst()) {
                Integer traceId = cursor.getInt(cursor.getColumnIndexOrThrow("traceId"));
                Integer producerId = cursor.getInt(cursor.getColumnIndexOrThrow("producerId"));
                byte[] producerImg = cursor.getBlob(cursor.getColumnIndexOrThrow("producerImg"));
                Integer dealerId = cursor.getInt(cursor.getColumnIndexOrThrow("dealerId"));
                byte[] dealerImg = cursor.getBlob(cursor.getColumnIndexOrThrow("dealerImg"));
                Integer consumerId = cursor.getInt(cursor.getColumnIndexOrThrow("consumerId"));
                byte[] consumerImg = cursor.getBlob(cursor.getColumnIndexOrThrow("consumerImg"));
                cursor.close();
                return new Trace(consumerId, consumerImg, dealerId, dealerImg, producerId, producerImg, productId, traceId);
            }

            return null;
        } finally {
            db.close();
        }
    }
}
